import time

from click import prompt
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver import Keys
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.action_chains import ActionChains


class Base():
    #初始化浏览器
    def __init__(self,driver):
        self.driver=driver

    #获取网址
    def open_url(self,url):
        self.driver.get('https://www.jiuxian.com/')

    #封装定位
    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,30,0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    #封装点击操作
    def click(self,loc):
        self.find_element(loc).click()

    #封装输入
    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    #封装断言
    def text(self,loc):
        return self.find_element(loc).text

    #封装切换窗口
    def window(self):
        # self.driver.switch_to.window(self.driver.window_handles[-1])
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #封装进入框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))

    #封装退出框架
    def frame_content(self):
        self.driver.switch_to.default_content()

    #封装下拉框
    def select_index(self,loc,x):
        index=Select(self.find_element(loc))
        index.select_by_index(x)
    def select_value(self,loc,x):
        value=Select(self.find_element(loc))
        value.select_by_value(x)
    def select_context(self,loc,x):
        context=Select(self.find_element(loc))
        context.select_by_visible_text(x)

    #封装滚动到任意像素
    def scrollTo_x(self,x):
        self.driver.execute_script(f'window scrollTo(0,{x})')
    #封装滚动到底部
    def scrollTo_H(self):
        self.driver.execute_script('window scrollTo(0,document.body.scrollHeight)')

    #封装弹窗
    def alert_accept(self):
        alert=self.driver.switch_to.alert
    def confirm_dismiss(self):
        confirm=self.driver.switch_to.alert
        confirm.dismiss()
    def prompt_accept(self):
        prompt=self.driver.switch_to.alert
        prompt.send_keys("这是一个accept弹窗")
        prompt.accept()
    def prompt_dismiss(self):
        prompt=self.driver.switch_to.alert
        prompt.send_keys("这是一个accept弹窗")
        prompt.dismiss()

    def alert_accept(self):
        alert=self.driver.switch_to.alert
    def confirm_dismiss(self):
        confirm=self.driver.switch_to.alert
        confirm.dismiss(self)
    def prompt_accept(self):
        prompt=self.driver.switch_to.alert
        prompt.send_keys('')
        prompt.accept()

    #封装获取时间戳并截屏
    def get_time(self):
        return time.strftime("%Y%m%d%H%M%S",time.localtime())
    def screenshot(self):
        self.driver.save_screenshot(self.get_time() + '.png')

    #封装鼠标单击
    def sbclick(self,loc):
        ActionChains(self.driver).click(self.find_element(loc)).perform()

    #封装键盘回车
    def jianpan(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)